<?php
/* 
 * Copyright (C) 2012 Leah Rowe <info@minifree.org>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

/*
 * This finds all servers in the database, and generates
 * links to oadisplay.php using parameters, e.g.
 - oadisplay.php?server=x&server=y
 
 * For servers currently listed as offline, it will still generate a link,
 * but the anchor text will have Offline in parenthesis.
 */
	
include "oaprotected/common.php";

function getOnline() 
{
	$output = "";

	$sqlServers = inject("SELECT * FROM servers WHERE serverdown = '0'");
	$sqlServersNrows = mysql_numrows($sqlServers);
	
	$sqlQStat = inject("SELECT * FROM qstat");
	$sqlQStatNrows = mysql_numrows($sqlQStat);		
	
	$qstatListURL = array($sqlQStatNrows);
	
	for ($i = 0; $i < $sqlQStatNrows; $i++) {
		$qstatListURL[$i] = mysql_result($sqlQStat, $i, "listurl");
	}
	
	for ($i = 0; $i < $sqlServersNrows; $i++) 
	{
		$sqlServersIPAddr = mysql_result($sqlServers, $i, "ipaddr");
		$sqlServersPortNum = mysql_result($sqlServers, $i, "portnum");
		
		$numplayers = "";
		$sv_hostname = "";
		
		for ($j = 0; $j < count($qstatListURL); $j++)
		{
			$content = curlGetPage($qstatListURL[$j].
			(strpos($qstatListURL[$j],"?")!==false?"&":"?")
			."server=$sqlServersIPAddr:$sqlServersPortNum&xml=1");
			
			$qstat = new SimpleXMLElement($content);
			
			// check if server is listed here (if not then it is
			// offline, but may be on another master server)
			$server_status = $qstat->server[0]['status'];
			if ($server_status != "UP") {
			
				continue;
			}
			
			$numplayers = $qstat->server[0]->numplayers;
			
			foreach($qstat->server[0]->rules->rule as $rule) {
				switch((string) $rule['name']) {
					case 'sv_hostname':
						$sv_hostname = $rule;
						break;
					default:
						continue;
				}
			}
		}
		
		$output .= "<li class='oachecker_li'>$sv_hostname<br/>Player ".
			"Count: $numplayers<br/><a class='oachecker_a' href".
			"='?ipaddr=$sqlServersIPAddr&portnum".
			"=$sqlServersPortNum'>$sqlServersIPAddr:".
			"$sqlServersPortNum</a></li>";
	}
	
	$output = "<ul class='oachecker_ul'>$output</ul>";
	return $output;	
}

function getOffline() 
{	
	$output = "";
	
	$sqlServers = inject("SELECT * FROM servers WHERE serverdown = '1'");
	$sqlServersNrows = mysql_numrows($sqlServers);
	
	for ($i = 0; $i < $sqlServersNrows; $i++) {
		$sqlServersIPAddr = mysql_result($sqlServers, $i, "ipaddr");
		$sqlServersPortNum = mysql_result($sqlServers, $i, "portnum");
		$output .= "<li class='oachecker_li'><a class='oachecker_a' h".
			"ref='?ipaddr=$sqlServersIPAddr&portnum=".
			"$sqlServersPortNum'>$sqlServersIPAddr:".
			"$sqlServersPortNum</a></li>";
	}
	
	$output = "<ul class='oachecker_ul'>$output</ul>";
	return $output;	
}
?>
